package java学习.leetcode.editor.cn;

import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * @author 刘世锦
 * 2022-11-21 21:32:09	 当前时间
 */
//给定一个二叉树的 根节点 root，想象自己站在它的右侧，按照从顶部到底部的顺序，返回从右侧所能看到的节点值。 
//
// 
//
// 示例 1: 
//
// 
//
// 
//输入: [1,2,3,null,5,null,4]
//输出: [1,3,4]
// 
//
// 示例 2: 
//
// 
//输入: [1,null,3]
//输出: [1,3]
// 
//
// 示例 3: 
//
// 
//输入: []
//输出: []
// 
//
// 
//
// 提示: 
//
// 
// 二叉树的节点个数的范围是 [0,100] 
// -100 <= Node.val <= 100 
// 
// Related Topics 树 深度优先搜索 广度优先搜索 二叉树 
// 👍 783 👎 0

public class 二叉树的右视图{
	public static void main(String[] args) {
		Solution solution = new 二叉树的右视图().new Solution();
		StringBuilder result = new StringBuilder();
		String param0 = "5025  ，110， -1 , 2 ，   3   4 ,   123";
		String regex = ",|，|\\s+";
		String[] split = param0.split(regex);
		System.out.println(Arrays.toString(split));
		for (int i = 0; i < split.length; i++) {
			if (split[i].length() > 0){
				result.append(split[i]);
				if (i<split.length-1){
					result.append(",");
				}

			}
		}
		System.out.println(result.toString());
	}
//leetcode submit region begin(Prohibit modification and deletion)
/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode() {}
 *     TreeNode(int val) { this.val = val; }
 *     TreeNode(int val, TreeNode left, TreeNode right) {
 *         this.val = val;
 *         this.left = left;
 *         this.right = right;
 *     }
 * }
 */
class Solution {
	/**
	 *
	 *  _  20    _
	 *  3 9
	 *  3 9 20 15 7
	 */
    public List<Integer> rightSideView(TreeNode root) {
		ArrayList<Integer> list = new ArrayList<>();
		ArrayDeque<TreeNode> deque = new ArrayDeque<>();
		if (root!=null){
			deque.offer(root);
			while (!deque.isEmpty()){
				int len = deque.size();
				for (int i = 0; i < len; i++) {
					TreeNode node = deque.pop();
					if (i==len-1){
						list.add(node.val);;
					}
					if (node.left!=null){
						deque.offer(node.left);
					}
					if (node.right!=null){
						deque.offer(node.right);
					}
				}
			}
		}
		return list;
	}
}
	public class TreeNode {
		int val;
		TreeNode left;
		TreeNode right;
		TreeNode() {}
		TreeNode(int val) { this.val = val; }
		TreeNode(int val, TreeNode left, TreeNode right) {
			this.val = val;
			this.left = left;
			this.right = right;
		}
	}
//leetcode submit region end(Prohibit modification and deletion)

}
